package org.jeecg.modules.wlhy.hyevent.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.util.PasswordUtil;
import org.jeecg.modules.third.amap.service.AmapService;
import org.jeecg.modules.wlhy.hyconfig.entity.HyConfig;
import org.jeecg.modules.wlhy.hyconfig.service.IHyConfigService;
import org.jeecg.modules.wlhy.setting.service.IHySettingService;
import org.jeecg.modules.wlhy.shipping.entity.HyOrdosReport;
import org.jeecg.modules.wlhy.shipping.service.IHyOrdosReportService;
import org.jeecg.modules.wlhy.utils.HyCfgUtil;
import org.jeecg.modules.wlhy.utils.HyConfigUtils;
import org.jeecg.modules.wlhy.utils.InitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


@RestController
@RequestMapping("/hyevent")
@Slf4j
public class HyEventController {

    @Autowired
    private IHyOrdosReportService hyOrdosReportService;

    @PostMapping(value = "/zjreport/callback")
    public Result<?> reportCallback(@RequestBody JSONObject jsonObject) {
        log.warn("中交上报回调消息，{}", JSONUtil.toJsonStr(jsonObject));
        return Result.OK();
    }

    @PostMapping(value = "/zjshippingfence/callback")
    public Result<?> shippingFenceCallback(@RequestBody JSONObject jsonObject) {
        log.warn("中交电子围栏回调消息，{}", JSONUtil.toJsonStr(jsonObject));
        return Result.OK();
    }

    /**
     * 鄂尔多斯监管平台回调
     * @param jsonObject
     * @return
     */
    @PostMapping(value = "/ordosreport/callback")
    public Result<?> ordosreportCallback(@RequestBody JSONObject jsonObject) {
        log.warn("鄂尔多斯监管平台回调，{}", JSONUtil.toJsonStr(jsonObject));

        HyOrdosReport hyOrdosReport = new HyOrdosReport();
        if (StrUtil.isNotEmpty(jsonObject.getString("shipmentCode"))) {
            if (jsonObject.getString("step").equals("装货核验")) {
                hyOrdosReport.setShippingNoteNumber(jsonObject.getString("shipmentCode"));
                hyOrdosReport.setLoadReportStatus(jsonObject.getString("status"));
                hyOrdosReport.setLoadReportTime(new Date());
                hyOrdosReport.setLoadReportResult(jsonObject.getString("content"));
            } else if (jsonObject.getString("step").equals("卸货核验")) {
                hyOrdosReport.setShippingNoteNumber(jsonObject.getString("shipmentCode"));
                hyOrdosReport.setUnloadReportStatus(jsonObject.getString("status"));
                hyOrdosReport.setUnloadReportTime(new Date());
                hyOrdosReport.setUnloadReportResult(jsonObject.getString("content"));
            } else if (jsonObject.getString("step").equals("上传轨迹核验")) {
                hyOrdosReport.setShippingNoteNumber(jsonObject.getString("shipmentCode"));
                hyOrdosReport.setTraceReportStatus(jsonObject.getString("status"));
                hyOrdosReport.setTraceReportTime(new Date());
                hyOrdosReport.setTraceReportResult(jsonObject.getString("content"));
            } else if (jsonObject.getString("step").equals("结算核验")) {
                hyOrdosReport.setShippingNoteNumber(jsonObject.getString("shipmentCode"));
                hyOrdosReport.setShippingReportStatus(jsonObject.getString("status"));
                hyOrdosReport.setShippingReportTime(new Date());
                hyOrdosReport.setShippingReportResult(jsonObject.getString("content"));
            } else if (jsonObject.getString("step").equals("上传资金流水核验")) {
                hyOrdosReport.setShippingNoteNumber(jsonObject.getString("shipmentCode"));
                hyOrdosReport.setPayReportStatus(jsonObject.getString("status"));
                hyOrdosReport.setPayReportTime(new Date());
                hyOrdosReport.setPayReportResult(jsonObject.getString("content"));
            } else if (jsonObject.getString("step").equals("开票核验")) {
                hyOrdosReport.setShippingNoteNumber(jsonObject.getString("shipmentCode"));
                hyOrdosReport.setInvonceReportStatus(jsonObject.getString("status"));
                hyOrdosReport.setInvonceReportTime(new Date());
                hyOrdosReport.setInvonceReportResult(jsonObject.getString("content"));
            }
        }

        if (StrUtil.isNotEmpty(hyOrdosReport.getShippingNoteNumber())) {
            HyOrdosReport reportDB = this.hyOrdosReportService.getOne(
                    new LambdaQueryWrapper<HyOrdosReport>().eq(HyOrdosReport::getShippingNoteNumber, hyOrdosReport.getShippingNoteNumber()));
            if (null == reportDB) {
                this.hyOrdosReportService.save(hyOrdosReport);
            } else {
                hyOrdosReport.setId(reportDB.getId());
                this.hyOrdosReportService.updateById(hyOrdosReport);
            }
        }

        return Result.OK("保存成功", hyOrdosReport.getShippingNoteNumber());
    }

}
